//
//  UITableView+YCTools.m
//  Pods
//
//  Created by Apple on 2022/11/3.
//

#import "UITableView+YCTools.h"
#import "UIView+YCTools.h"

@implementation UITableView (YCTools)

+ (UITableView *)tableViewWithFrame:(CGRect)frame
                           delegate:(id)delegate
                         dataSource:(id)dataSource {
    return [self tableViewWithFrame:frame bgColor:UIColor.clearColor separatorStyle:UITableViewCellSeparatorStyleNone delegate:delegate dataSource:dataSource];
}

+ (UITableView *)tableViewWithFrame:(CGRect)frame
                              style:(UITableViewStyle)style
                           delegate:(id)delegate
                         dataSource:(id)dataSource {
    return [self tableViewWithFrame:frame style:style bgColor:UIColor.clearColor separatorStyle:UITableViewCellSeparatorStyleNone delegate:delegate dataSource:dataSource];
}

+ (UITableView *)tableViewWithFrame:(CGRect)frame
                            bgColor:(UIColor *)bgColor
                           delegate:(id)delegate
                         dataSource:(id)dataSource {
    return [self tableViewWithFrame:frame bgColor:bgColor separatorStyle:UITableViewCellSeparatorStyleNone delegate:delegate dataSource:dataSource];
}

+ (UITableView *)tableViewWithFrame:(CGRect)frame
                            bgColor:(UIColor *)bgColor
                     separatorStyle:(UITableViewCellSeparatorStyle)separatorStyle
                           delegate:(id)delegate
                         dataSource:(id)dataSource {
    return [self tableViewWithFrame:frame style:UITableViewStylePlain bgColor:bgColor separatorStyle:separatorStyle delegate:delegate dataSource:dataSource];
}

+ (UITableView *)tableViewWithFrame:(CGRect)frame
                              style:(UITableViewStyle)style
                            bgColor:(UIColor *)bgColor
                     separatorStyle:(UITableViewCellSeparatorStyle)separatorStyle
                           delegate:(id)delegate
                         dataSource:(id)dataSource {
    
    UITableView *tableView = [[UITableView alloc] initWithFrame:frame style:style];
    tableView.backgroundColor = bgColor;
    tableView.dataSource = delegate;
    tableView.delegate = dataSource;
    tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, 40)];
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    return tableView;
}

- (void)setupSlideButtonWithFont:(UIFont *)font textColor:(UIColor *)textColor radius:(CGFloat)radius {
    // 判断系统是否是 iOS13 及以上版本
    if (@available(iOS 13.0, *)) {
        for (UIView *subView in self.subviews) {
            if ([subView isKindOfClass:NSClassFromString(@"_UITableViewCellSwipeContainerView")] && [subView.subviews count] >= 1) {
                // 修改图片
                UIView *remarkContentView = subView.subviews.firstObject;
                [self setupRowActionView:remarkContentView font:font textColor:textColor radius:radius];
            }
        }
        return;
    }

    // 判断系统是否是 iOS11 及以上版本
    if (@available(iOS 11.0, *)) {
        for (UIView *subView in self.subviews) {
            if ([subView isKindOfClass:NSClassFromString(@"UISwipeActionPullView")] && [subView.subviews count] >= 1) {
                // 修改图片
                UIView *remarkContentView = subView;
                [self setupRowActionView:remarkContentView font:font textColor:textColor radius:radius];
            }
        }
        return;
    }
}

- (void)setupRowActionView:(UIView *)rowActionView font:(UIFont *)font textColor:(UIColor *)textColor radius:(CGFloat)radius {
    [rowActionView radiusWithRadius:radius corner:UIRectCornerTopRight | UIRectCornerBottomRight];
    
    for (UIButton *btn in rowActionView.subviews) {
        btn.titleLabel.font = font;
        [btn setTitleColor:textColor forState:UIControlStateNormal];
    }
}

@end
